﻿Imports Microsoft.VisualBasic
Imports Aras.IOM
Imports MSXML2
Imports System.Xml
Imports System.IO





Public Class ConnecteurAras

    Dim conn As HttpServerConnection
    Dim inn As Innovator
    Public InnovUser As String = ""
    Public pass As String = ""
    Public url As String = ""
    Public db As String = ""
    Private hashpass As String

    Public Sub New()

        pass = Innovator.ScalcMD5(pass)
        conn = IomFactory.CreateHttpServerConnection(url, db, InnovUser, pass)
        Dim login_result As Item = conn.Login


        If login_result.isError Then
            Throw New Exception("login failed")
        End If

        inn = IomFactory.CreateInnovator(conn)

    End Sub

    Public Sub openfile(ByVal ID As String)

        'récupréation de l'ID du document selectionné 
        Dim openitem As Item = Me.inn.getItemById("document", ID)
        'recherche de l'ID du fichier
        'récupérer l'URL du fichier
        'télécharger le fichier




    End Sub

    Public Function getfichiers(ByVal IDfolder As String) As String


        Dim qryitem As Item = Me.inn.newItem("DossierCollab", "get")
        qryitem.setProperty("id", IDfolder)
        Dim rel1 As Item = Me.inn.newItem("DossierDocument", "get")
        Dim subfolder1 As Item = Me.inn.newItem("Document", "get")
        subfolder1.setAttribute("select", " keyed_name, major_rev,name , state, classification")

        Dim phraseretour As String = "test"
        rel1.setRelatedItem(subfolder1)
        qryitem.addRelationship(rel1)

        Dim results As Item = qryitem.apply()

        Dim traitementXML As DOMDocument = New DOMDocument

        phraseretour = ""
        phraseretour += "<?xml version='1.0' encoding='utf-8' ?><rows>"
        Dim a As Integer
        Dim countsub As Integer = 0

        Dim item As Item = results.getItemByIndex(0)
        Try

            countsub = item.getRelationships.getItemCount

        Catch ex As Exception
            GoTo NoFichier
        End Try
        For a = 0 To countsub - 1
            Dim subitem1 As Item = item.getRelationships.getItemByIndex(a)
            Dim subitem2 As Item = subitem1.getRelatedItem.getItemByIndex(0)

            phraseretour += "<row id='" & subitem2.getID & "'>"
            phraseretour += "<cell>" & subitem2.getProperty("keyed_name") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("major_rev") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("name") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("classification") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("state") & "</cell>"
            phraseretour += "</row>"

        Next






        'Dim writer As XmlTextWriter = New XmlTextWriter("TempXML/grid_" & IDfolder & ".xml", Nothing)
        'Dim fsout As IO.FileStream = New IO.FileStream("step4.xml", FileMode.Create)
        'Dim chemin As String = System.AppDomain.CurrentDomain.BaseDirectory()
        'Dim writer As XmlTextWriter = New XmlTextWriter(chemin & "/TempXML/grid_" & IDfolder & ".xml", System.Text.Encoding.UTF8)
        'writer.Formatting = Formatting.Indented
        'Dim doc As XmlDocument = New XmlDocument
        'doc.LoadXml(phraseretour)
        'doc.save(writer)
NoFichier:
        phraseretour += "</rows>"
        Return phraseretour


    End Function

    Public Function addfile(ByVal IDfolder As String, ByVal FileName As String, ByVal filepath As String) As String
        Dim result As String = ""

        Dim qryitem As Item = Me.inn.newItem("DossierCollab", "edit")
        qryitem.setAttribute("select", "nomdossier")
        qryitem.setAttribute("where", "[DossierCollab].id='" & IDfolder & "'")
        Dim rel As Item = Me.inn.newItem("DossierDocument", "add")
        Dim subitem As Item = Me.inn.newItem("Document", "add")
        subitem.setProperty("name", FileName)
        subitem.setProperty("item_number", FileName)
        Dim docfile As Item = Me.inn.newItem("Document File", "add")
        Dim fileitem As Item = Me.inn.newItem("File", "add")
        fileitem.setProperty("filename", FileName)
        fileitem.attachPhysicalFile(filepath)

        docfile.setRelatedItem(fileitem)
        subitem.addRelationship(docfile)
        rel.setRelatedItem(subitem)
        qryitem.addRelationship(rel)

        Dim results As Item = qryitem.apply()



        'test si existance d'une erreur
        result = Me.getdossiers()


        Return result

    End Function


    Public Function Downloadsimplefile(ByVal fileID As String, ByVal checkoutpath As String) As String

       




    End Function

    Public Function addsimplefile(ByVal FileName As String, ByVal filepath As String) As String
        Dim result As String = ""


        Dim fileitem As Item = Me.inn.newItem("File", "add")
        fileitem.setProperty("filename", FileName)
        fileitem.attachPhysicalFile(filepath)

        Dim results As Item = fileitem.apply()

        'unused but should return a statement on the results item
        Return result

    End Function

    Public Function getdossiers() As String

        Dim qryitem As Item = Me.inn.newItem("DossierCollab", "get")
        qryitem.setAttribute("select", "nomdossier")
        qryitem.setProperty("isrootfolder", "1")
        Dim rel1 As Item = Me.inn.newItem("DossierSousDos", "get")
        Dim subfolder1 As Item = Me.inn.newItem("DossierCollab", "get")
        Dim rel2 As Item = Me.inn.newItem("DossierSousDos", "get")
        Dim subfolder2 As Item = Me.inn.newItem("DossierCollab", "get")
        Dim rel3 As Item = Me.inn.newItem("DossierSousDos", "get")
        Dim subfolder3 As Item = Me.inn.newItem("DossierCollab", "get")
        Dim rel4 As Item = Me.inn.newItem("DossierSousDos", "get")
        Dim subfolder4 As Item = Me.inn.newItem("DossierCollab", "get")
        Dim rel5 As Item = Me.inn.newItem("DossierSousDos", "get")
        Dim subfolder5 As Item = Me.inn.newItem("DossierCollab", "get")
        Dim rel6 As Item = Me.inn.newItem("DossierSousDos", "get")
        Dim subfolder6 As Item = Me.inn.newItem("DossierCollab", "get")
        rel6.setRelatedItem(subfolder6)
        subfolder5.addRelationship(rel6)
        rel5.setRelatedItem(subfolder5)
        subfolder4.addRelationship(rel5)
        rel4.setRelatedItem(subfolder4)
        subfolder3.addRelationship(rel4)
        rel3.setRelatedItem(subfolder3)
        subfolder2.addRelationship(rel3)
        rel2.setRelatedItem(subfolder2)
        subfolder1.addRelationship(rel2)
        rel1.setRelatedItem(subfolder1)
        qryitem.addRelationship(rel1)
        Dim results As Item = qryitem.apply()





        Dim count As Integer = results.getItemCount
        Dim a, b, c, d, e, f, g As Integer

        Dim content As String = "<?xml version='1.0' ?><tree id='0'>"
        Dim countsub1, countsub2, countsub4, countsub6 As Integer


        For a = 0 To count - 1
            Dim item As Item = results.getItemByIndex(a)
            content += "" & _
            "<item id='" & item.getProperty("id") & "' text='" & item.getProperty("nomdossier") & "'>"
            countsub1 = item.getRelationships.getItemCount
            For b = 0 To countsub1 - 1
                Dim subitem1 As Item = item.getRelationships.getItemByIndex(b)
                countsub2 = subitem1.getItemCount
                Dim subitem2 As Item = subitem1.getRelatedItem.getItemByIndex(0)
                content += "" & _
                "<item id='" & subitem2.getProperty("id") & "' text='" & subitem2.getProperty("nomdossier") & "'>"
                countsub2 = subitem2.getRelationships.getItemCount
                For c = 0 To countsub2 - 1
                    Dim subitem3 As Item = subitem2.getRelationships.getItemByIndex(c)
                    countsub4 = subitem3.getItemCount
                    Dim subitem4 As Item = subitem3.getRelatedItem.getItemByIndex(0)
                    content += "" & _
                    "<item id='" & subitem4.getProperty("id") & "' text='" & subitem4.getProperty("nomdossier") & "'>"
                    countsub4 = subitem4.getRelationships.getItemCount
                    For e = 0 To countsub4 - 1
                        Dim subitem5 As Item = subitem4.getRelationships.getItemByIndex(e)
                        countsub6 = subitem5.getItemCount
                        Dim subitem6 As Item = subitem5.getRelatedItem.getItemByIndex(0)
                        content += "" & _
                        "<item id='" & subitem6.getProperty("id") & "' text='" & subitem6.getProperty("nomdossier") & "'>"

                        content += "" & _
                        "</item>"
                    Next
                    content += "" & _
                    "</item>"
                Next
                content += "" & _
                "</item>"
            Next
            content += "" & _
            "</item>"
        Next


        content = content & "</tree>"
        Return content
    End Function


    Public Function newfolder(ByVal IDfolder As String, ByVal foldername As String, ByVal isroot As Boolean) As String

        Dim result As String = ""
        Dim qryitem As Item = Me.inn.newItem("DossierCollab", "edit")
        qryitem.setAttribute("select", "nomdossier")
        qryitem.setAttribute("where", "[DossierCollab].id='" & IDfolder & "'")
        Dim rel As Item = Me.inn.newItem("DossierSousDos", "add")
        Dim subitem As Item = Me.inn.newItem("DossierCollab", "add")
        subitem.setProperty("nomdossier", foldername)

        If isroot = False Then
            subitem.setProperty("isrootfolder", "0")
        Else
            subitem.setProperty("isrootfolder", "1")
        End If

        rel.setRelatedItem(subitem)
        qryitem.addRelationship(rel)

        Dim results As Item = qryitem.apply()

        'test si existance d'une erreur
        result = Me.getdossiers()


        Return result
    End Function

    Protected Overrides Sub Finalize()
        conn.Logout()
        MyBase.Finalize()
    End Sub
End Class
